package cs4280.ass2.admin.action;

import java.io.IOException;
import java.text.ParseException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang.time.DateUtils;

import cs4280.ass2.entity.Movie;
import cs4280.ass2.exception.ValidateException;
import cs4280.ass2.persistence.MovieDao;
import cs4280.ass2.util.MovieUtils;

public class MovieEditAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final String START_DATE_FORMAT = "dd/MM/yyyy";

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String id = req.getParameter("id");
		if (StringUtils.isBlank(id)) {
			resp.sendRedirect("list.do");
		} else {
			Movie movie = MovieDao.getInstance().retrieveMovie(Long.valueOf(id));
			req.setAttribute("movie", movie);
			getServletContext().getRequestDispatcher("/admin/movie/edit.jsp").forward(req, resp);
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String id = StringUtils.trim(req.getParameter("id"));
		String name = StringUtils.trim(req.getParameter("name"));
		String description = StringUtils.trim(req.getParameter("description"));
		String director = StringUtils.trim(req.getParameter("director"));
		String cast = StringUtils.trim(req.getParameter("cast"));
		String duration = StringUtils.trim(req.getParameter("duration"));
		String category = StringUtils.trim(req.getParameter("category"));
		String language = StringUtils.trim(req.getParameter("language"));
		String trailerUrl = StringUtils.trim(req.getParameter("trailerUrl"));
		String imageUrl = StringUtils.trim(req.getParameter("imageUrl"));
		String startDateStr = StringUtils.trim(req.getParameter("startDate"));

		Movie movie = MovieDao.getInstance().retrieveMovie(NumberUtils.toLong(id, 0));

		if (movie != null) {
			Date startDate = null;
			try {
				startDate = DateUtils.parseDate(startDateStr, new String[] { START_DATE_FORMAT });
			} catch (ParseException e) {
				req.setAttribute("message.error", "message.error.invalid.dateformat");
			}

			movie.setImageUrl(imageUrl);
			movie.setName(name);
			movie.setDescription(description);
			movie.setDirector(director);
			movie.setCast(cast);
			movie.setDuration(NumberUtils.toInt(duration, 0));
			movie.setCategory(category);
			movie.setLanguage(language);
			movie.setStartDate(startDate);
			movie.setTrailerUrl(trailerUrl);

			try {
				MovieUtils.validate(movie);
				MovieDao.getInstance().updateMovie(movie);
				req.setAttribute("message.info", "message.info.movie.updatesuccess");
				getServletContext().getRequestDispatcher("/admin/movie/list.do").forward(req, resp);
			} catch (ValidateException e) {
				req.setAttribute("message.error", e.getMessage());
				// set it back to request so that changes can be preserve in the
				// form.
				req.setAttribute("movie", movie);
				getServletContext().getRequestDispatcher("/admin/movie/edit.jsp").forward(req, resp);
			}
		} else {
			// input id not found, just redirect to list.do
			resp.sendRedirect("list.do");
		}
	}
}
